package com.gs.controller;

import java.sql.Timestamp;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gs.pojo.Attribute;
import com.gs.pojo.Passenger;
import com.gs.pojo.Predetermine;
import com.gs.pojo.ReceiveTarget;
import com.gs.pojo.RoomSet;
import com.gs.pojo.StayRegister;
import com.gs.service.attribute.AttributeService;
import com.gs.service.passenger.PassengerService;
import com.gs.service.predetermine.PredetermineService;
import com.gs.service.receiveTarget.ReceiveTargetService;
import com.gs.service.roomSet.RoomSetService;
import com.gs.service.stayregister.StayRegisterService;
import com.gs.utils.Page;

@Controller
@RequestMapping("/Predetermine")
public class PredetermineController {

	@Resource
	private PredetermineService predetermineService;

	@Resource
	private AttributeService attributeService;

	@Resource
	private ReceiveTargetService receiveTargetService;

	@Resource
	private PassengerService passengerService;

	@Resource
	private RoomSetService roomSetService;
	
	@Resource
	private StayRegisterService stayRegisterService;

	int idP[];     //预订id
	int fangJianId[];

	@RequestMapping(value="/tolist")
	public String tolist(Integer currentPage,
			String txtname,
			Integer state,
			Model model){
		Date date=new Date();
		DateFormat dformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //设置日期格式
		Timestamp timestamp=Timestamp.valueOf(dformat.format(date)) ;  //将当前时间转为字符串

		List<Predetermine> list=predetermineService.selectall();
		long shiJianCha;
		for (int i = 0; i < list.size(); i++) {
			Timestamp huoQuShiJian=list.get(i).getArriveTime();          //获取到达时间
			shiJianCha=timestamp.getTime()-huoQuShiJian.getTime();        //计算时间差
			if (shiJianCha>=0) {
				Integer huoQuId=list.get(i).getId();
				predetermineService.updateRemind(huoQuId);
			}
		}

		if (currentPage==null) {
			currentPage=1;
		}else if (currentPage==0) {
			currentPage=1;
		}
		if(txtname==null){ //默认没有模糊查询
			txtname="";
		}
		if(state==null){//默认未安排
			state=66;
		}
		List<Attribute> listOne=attributeService.selectPredetermineState();
		Page<Predetermine> vo=new Page<Predetermine>();
		vo.setCurrentPage(currentPage);
		vo=this.predetermineService.pageFuzzyselect(txtname, txtname, state, vo);
		model.addAttribute("listOne",listOne);
		model.addAttribute("txtname",txtname);
		model.addAttribute("state",state);
		model.addAttribute("list",vo);
		return "/predetermine/list";
	}

	//ajax加载团队信息
	@RequestMapping(value="selectTarget")
	@ResponseBody
	public Object selectTarget(String txtname){
		if(txtname==null){
			txtname="";
		}
		List<ReceiveTarget> list=receiveTargetService.ajaxSelect(txtname);
		return list;
	}

	//ajax加载旅客信息
	@RequestMapping(value="/selectPassenger")
	@ResponseBody
	public Object selectPassenger(String txtname){
		if(txtname==null){
			txtname="";
		}
		List<Passenger> list=passengerService.selectAjaxList(txtname);
		return list;
	}

	//
	@RequestMapping(value="/confirmPassenger")
	@ResponseBody
	public Object confirmPassenger(Integer id){
		Passenger list=passengerService.selectById(id);
		return list.getContactPhoneNumber();
	}


	//去新增界面
	@RequestMapping("/toadd")
	public String toadd(Integer id,
			String name,
			Integer type,
			Model model){
		List<Attribute> listOne=attributeService.selectPayWay();
		model.addAttribute("id",id);
		model.addAttribute("name",name);
		model.addAttribute("type",type);
		model.addAttribute("listOne",listOne);
		return "/predetermine/add";
	}

	//新增处理
	@RequestMapping("/add")
	public String add(Integer id,
			Integer type, 
			String roomIdShuZu,
			Predetermine po,
			Integer pangduan,
			RoomSet roomSet,
			Model model){
		if(pangduan!=null){
			for (int i = 0; i < idP.length; i++) {
				predetermineService.deleteById(idP[i]);
			}
			roomSet.setRoomStateID(1);                       //将此房态设置为空房
			for (int i = 0; i < fangJianId.length; i++) {
				roomSet.setId(fangJianId[i]);
				roomSetService.updateByIdToRoomState(roomSet);    //修改此房态
			}
		}

		String[] FenGe=roomIdShuZu.split(",");             //分割为数组
		int changDu=FenGe.length;                          //获取数组长度
		double yaJin=po.getDeposit();                      //获取押金             
		double yaJinFenGe=yaJin/changDu;                   //得到平均押金
		po.setDeposit(yaJinFenGe);                         //设po的押金
		po.setPredetermineStateID(66);                     //设置预订状态为未按排

		if(type==1){                                       //如果为团队
			po.setPassengerID(0);                          //设置预订旅客id为0
			po.setPredetermineTargetID(id);                //设置团队id
		}else if(type==2){                                 //判断是否为旅客
			po.setPassengerID(id);                         //给预订赋值旅客id
			po.setPredetermineTargetID(2);                 //设置团队id
		}

		roomSet.setRoomStateID(4);                       //设置此房态为预订
		for(int i=0;i<changDu;i++){
			po.setRoomID(Integer.parseInt(FenGe[i]));      //给预订设置房间id
			predetermineService.insertAll(po);             //新增预订单
			roomSet.setId(Integer.parseInt(FenGe[i]));   //更改房态的 获取房间id
			roomSetService.updateByIdToRoomState(roomSet);//修改房态为预订
		}

		return "redirect:/Predetermine/tolist.do";
	}


	//新增预定选择房间
	@RequestMapping(value="/selectRoom")
	@ResponseBody
	public Object selectRoom(String roomNumber){
		if(roomNumber==null){
			roomNumber="";
		}
		List<RoomSet> list=roomSetService.selectInformation(roomNumber);
		return list;
	}

	//去修改界面
	@RequestMapping("/toupdate")
	public String toupdate(Integer id,
			Model model){
		idP=null;
		fangJianId=null;
		int idid=0;
		Predetermine predetermine=predetermineService.findById(id);    //根据预订id来查询预订数据
		int lvKeId=predetermine.getPassengerID();                      //获取预订旅客id
		int teamId=predetermine.getPredetermineTargetID();             //获取预订团队id
		List<Predetermine> teamList=null;
		List<Predetermine> lvKeList=null;
		List<Attribute> listOne=attributeService.selectPayWay();
		double yaJin=0;
		int zhengShu=0;
		String nameString="";
		List<RoomSet> roomSetPolist=new ArrayList<RoomSet>();
		RoomSet roomSet=null;
		int type=0;

		if (lvKeId==0) {
			idid=teamId;
			teamList=predetermineService.findTeamId(teamId);
			idP=new int[teamList.size()];
			fangJianId=new int[teamList.size()];
			for (int i = 0; i < teamList.size(); i++) {
				yaJin+=teamList.get(i).getDeposit();
				roomSet=roomSetService.selectById(teamList.get(i).getRoomID());
				roomSetPolist.add(roomSet);
				idP[i]=teamList.get(i).getId();
				fangJianId[i]=teamList.get(i).getRoomID();
			}
			zhengShu=(int)yaJin;
			model.addAttribute("listList",teamList);
			if(teamList.size()>0){
				nameString=teamList.get(0).getReceiveTeamName();
			}
			
			type=1;
		}else {
			idid=lvKeId;
			lvKeList=predetermineService.findLvKeId(lvKeId);
			idP=new int[lvKeList.size()];
			fangJianId=new int[lvKeList.size()];
			for (int i = 0; i < lvKeList.size(); i++) {
				yaJin+=lvKeList.get(i).getDeposit();
				roomSet=roomSetService.selectById(lvKeList.get(i).getRoomID());
				roomSetPolist.add(roomSet);
				idP[i]=lvKeList.get(i).getId();
				fangJianId[i]=lvKeList.get(i).getRoomID();
			}
			zhengShu=(int)yaJin;
			model.addAttribute("listList",lvKeList);
			if(lvKeList.size()>0){
				nameString=lvKeList.get(0).getPassengerName(); 
			}
			
			type=2;
			
		}

		model.addAttribute("id",idid);
		model.addAttribute("listOne",listOne);
		model.addAttribute("roomSetPolist",roomSetPolist);
		model.addAttribute("zhengShu",zhengShu);
		model.addAttribute("name",nameString);
		model.addAttribute("type",type);
		model.addAttribute("pangduan",1);

		return "/predetermine/update";
	}

	//修改房间，移除房间时更改房态
	@RequestMapping("/delete")
	public String delete(String id){
		String[] FenGe=id.split(",");
		for (int i = 0; i < FenGe.length; i++) {
			this.predetermineService.deleteById(Integer.parseInt(FenGe[i]));
		}
		return "redirect:/Predetermine/tolist.do";
	}
	
	//安排房间
	@RequestMapping("/arrangeRoom")
	public String arrangeRoom(String id,
							 Integer tiaoZhuang,
							 Model model,
							 StayRegister stayRegister,
							 RoomSet roomSet){
		Date date=new Date();
		DateFormat dformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //设置日期格式
		Timestamp timestamp=Timestamp.valueOf(dformat.format(date)) ;  //将当前时间转为字符串
		
		String[] FenGe=id.split(",");                                    //分割到数组
		for (int i = 0; i < FenGe.length; i++) {
            predetermineService.updatePredetermineStateID(Integer.parseInt(FenGe[i]));              //修改预订状态
            Predetermine predetermine=predetermineService.findById(Integer.parseInt(FenGe[i])); //查询预订信息
            RoomSet roomSetPo=roomSetService.selectById(predetermine.getRoomID());              //查询房间信息
            stayRegister.setRoomID(predetermine.getRoomID());                                        //设置房间id
            stayRegister.setRentOutTypeID(26);
            stayRegister.setPassengerTypeID(29);
            if (predetermine.getPassengerID()==0) { //判断是否为团队还是散客
            	stayRegister.setBillUnitID(28);//团队
			}else {
				stayRegister.setBillUnitID(27);//旅客
			}
            stayRegister.setReceiveTargetID(predetermine.getPredetermineTargetID());
            stayRegister.setIsBillID(68);
            stayRegister.setRegisterTime(timestamp);
            stayRegister.setStayNumber(predetermine.getPredetermineDay());
            stayRegister.setSumConst(roomSet.getStandardPriceDay()*Integer.parseInt(predetermine.getPredetermineDay()));  //获取房价 乘以 住宿天数
            stayRegisterService.insertAll(stayRegister);  //新增成功时，获取刚新增的id。
            Integer stayId=stayRegister.getId();
            stayRegisterService.updateStayRegisterPredetermineID(Integer.parseInt(FenGe[i]), stayId);
            stayRegister.setDepositStayRegisterID(stayId);
    		stayRegister.setDepositRegisterTime(timestamp);
    		stayRegister.setDepositPayWayID(predetermine.getPayWayID());
    		stayRegister.setDeposit(predetermine.getDeposit());
    		stayRegisterService.insertDeposit(stayRegister);
    		roomSet.setId(predetermine.getRoomID());      //给新的 Po 赋房间ID的值
    		roomSet.setRoomStateID(65);                     //给新的 Po 赋房态的值
    		roomSetService.updateByIdToRoomState(roomSet);  //根据 房间ID 来修改 当前被选中的房间的房态
    	}
		if (tiaoZhuang==1) {
			return"redirect:/StayRegister/tolist.do";
		}else if (tiaoZhuang==2){
			return"redirect:/Predetermine/tolist.do";
		}
		return "redirect:/Predetermine/tolist.do";
	}
	

}
